package com.skivingcloud.ai.controller;

import com.alibaba.dashscope.audio.tts.SpeechSynthesisAudioFormat;
import com.skivingcloud.ai.constants.AiConstants;
import com.skivingcloud.ai.service.AiCommonApiService;
import com.skivingcloud.common.utils.UuidUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.ai.image.ImageResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * @author hushouquan
 * @since 2024/5/22
 */
@RestController
@Tag(name = "AI接口")
@RequestMapping(AiConstants.AI_BASE_URL + "/aiCommonApi")
public class AiCommonApiController{
    
    @Resource
    private Map<String, AiCommonApiService> aiCommonApiService;
    
    @Value("${skivingcloud.ai.chat,name:tongYi}") 
    private String chatName;
    
    /**
     * 简单对话
     * @param text 问题
     * @return 回答
     */
    @PostMapping("/completion")
    @Operation(summary="AI简单对话")
    public Mono<String> completion(@RequestBody String text){
        String result = aiCommonApiService.get(chatName + AiConstants.AI_CHAT_SERVICE_END_TEXT).completion(text);
        return Mono.just(result);
    }
    
    /**
     * 连续对话
     * @param text 问题
     * @return 回答
     */
    @PostMapping("/streamCompletion")
    @Operation(summary="AI连续对话")
    public Mono<Map<String, String>> streamCompletion(@RequestBody String text){
        Map<String, String> result = aiCommonApiService.get(chatName + AiConstants.AI_CHAT_SERVICE_END_TEXT).streamCompletion(text);
        return Mono.just(result);
    }
    
    /**
     * 生成图片
     * @param text 问题
     * @return 图片
     */
    @PostMapping("/genImg")
    @Operation(summary="AI生成图片")
    public Mono<ImageResponse> genImg(@RequestBody String text, HttpServletResponse response){
        ImageResponse result = aiCommonApiService.get(chatName + AiConstants.AI_CHAT_SERVICE_END_TEXT).genImg(text);
        return Mono.just(result);
    }
    
    /**
     * 生成语音
     * @param text 问题
     * @return 语音
     */
    @PostMapping("/genAudio")
    @Operation(summary="AI生成语音")
    public Mono<String> genAudio(@RequestBody String text, HttpServletResponse response) throws IOException{
        response.setContentType("application/octet-stream;charset=UTF-8");
        response.setHeader("Content-Disposition",
                "attachment;filename=" + java.net.URLEncoder.encode(UuidUtil.get16NumUUID() + "."
                        + SpeechSynthesisAudioFormat.WAV.getValue(), StandardCharsets.UTF_8));//
        response.addHeader("Pargam", "no-cache");
        response.addHeader("Cache-Control", "no-cache");
        ByteBuffer result = aiCommonApiService.get(chatName + AiConstants.AI_CHAT_SERVICE_END_TEXT).genAudio(text);
        OutputStream os = response.getOutputStream();
        os.write(result.array());
        return Mono.just("下载完成");
    }
}
